﻿/*
 * Class: Custom Sorting Addon
 * Author: Mike Cheng
 * 
 * Addition to ObjectListView
 * 
 * The following class allows for custom sorting in the ObjectListView.
 * 
 * You can change parameters and such the CustomSort method, or you can add your
 * own, and change the sort in Comparer.cs (Function: public int Compare(OLVListItem x, OLVListItem y) (Line:71))
 * 
 * Current Custom Sort ignore's "the" and "a" prefix's.
 */

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace BrightIdeasSoftware
{
    class Sorting
    {
        static public void CustomSort(object x, object y)
        {
            string[] px = Convert.ToString(x).Split(' ');
            string[] py = Convert.ToString(y).Split(' ');
            string newX = "";
            string newY = "";

            if (px.Length > 1)
            {
                if ((px[0].ToLower() == "a") || (px[0].ToLower() == "the"))
                {
                    for (int i = 1; i < px.Length; i++)
                    {
                        if (i == px.Length - 1)
                            newX += px[i];
                        else
                            newX += px[i] + " ";
                    }
                }
                else
                {
                    for (int i = 0; i < px.Length; i++)
                    {
                        if (i == px.Length - 1)
                            newX += px[i];
                        else
                            newX += px[i] + " ";
                    }
                }
            }
            else
            {
                for (int i = 0; i < px.Length; i++)
                {
                    if (i == px.Length - 1)
                        newX += px[i];
                    else
                        newX += px[i] + " ";
                }
            }

            if (py.Length > 1)
            {
                if ((py[0].ToLower() == "a") || (py[0].ToLower() == "the"))
                {
                    for (int i = 1; i < py.Length; i++)
                    {
                        if (i == py.Length - 1)
                            newY += py[i];
                        else
                            newY += py[i] + " ";
                    }
                }
                else
                {
                    for (int i = 0; i < py.Length; i++)
                    {
                        if (i == py.Length - 1)
                            newY += py[i];
                        else
                            newY += py[i] + " ";
                    }
                }
            }
            else
            {
                for (int i = 0; i < py.Length; i++)
                {
                    if (i == py.Length - 1)
                        newY += py[i];
                    else
                        newY += py[i] + " ";
                }
            }

            X = (object) newX;
            Y = (object) newY;
        }
        static public object GetX()
        {
            string StrX = Convert.ToString(X);
            string StrY = Convert.ToString(Y);
            bool isXNum = false;
            bool isYNum = false;
            double NumX;
            double NumY;

            if ((StrX.Length > 0) && (StrY.Length > 0))
            {
                isXNum = double.TryParse(StrX.Substring(0, 1), out NumY);
                isYNum = double.TryParse(StrY.Substring(0, 1), out NumX);
            }

            if (isXNum == true)
            {
                if (isYNum == true)
                    return X;
                else
                    return Y;
            }
            else if (isXNum == false)
            {
                if (isYNum == true)
                    return Y;
                else
                    return X;
            }
            else
                return X;
        }
        static public object GetY()
        {
            string StrX = Convert.ToString(X);
            string StrY = Convert.ToString(Y);
            bool isXNum = false;
            bool isYNum = false;
            double NumX;
            double NumY;

            if ((StrX.Length > 0) && (StrY.Length > 0))
            {
                isXNum = double.TryParse(StrX.Substring(0, 1), out NumY);
                isYNum = double.TryParse(StrY.Substring(0, 1), out NumX);
            }

            if (isXNum == true)
            {
                if (isYNum == true)
                    return Y;
                else
                    return X;
            }
            else if (isXNum == false)
            {
                if (isYNum == true)
                    return X;
                else
                    return Y;
            }
            else
                return Y;
        }
        static private object X;
        static private object Y;
    }
}
